<template>
  <div id="app"></div>
</template>

<script>
import axios from "axios";
import { eneryEquipmentQuery } from "./utils/api";
import { TUVector3, TURotator } from "tumap/dist/tumap.v1.8.js";
export default {
  name: "App",
  data() {
    return {
      systemData: []
    };
  },
  mounted() {
    // axios.get('./static/json/allUEEquipment.json').then((res)=> {
    //   let tem = res.data;
    //   let arr = []
    //   let list = ['消防栓']
    //   tem.forEach(item => {
    //     for(var i =0;i<list.length;i++){
    //       if(item.name.includes(list[i])){
    //         item.label = item.name;
    //         arr.push(item)
    //       }
    //     }
        
    //   })
    //   console.log(JSON.stringify(arr))
    // })
    this.getTree();
    let _tedp = new this.$trame({
      gisType: "ue",
      gisrefer: {
        ueUrl: window.ipUrl
      },
      width: 4096,
      height: 2304,
      navtop: 160,
      prop: [1071, 1075],
      showSub: true,
      nav: {
        navTop: () => import("@/components/top/navTop")
      },
      compss: [
        {
          sub: "首页",
          left: {
            left: () => import("@/components/left/index/index.vue")
          },
          right: {
            right: () => import("@/components/right/index/index.vue")
          }
        },
        {
          sub: "安全管理",
          left: {
            left: () => import("@/components/left/safetyManage/index.vue")
          },
          right: {
            right: () => import("@/components/right/safetyManage/Safety.vue")
          }
        },
        {
          sub: "设备管理",
          left: {
            left: () => import("@/components/left/equipmentManage/index.vue")
          },
          right: {
            right: () =>
              import("@/components/right/equipmentManage/equipmentTotal.vue")
          }
        },
        {
          sub: "运营管理",
          left: {
            left: () => import("@/components/left/operateManage/index.vue")
          },
          right: {
            right: () => import("@/components/right/operateManage/Operate.vue")
          }
        },
        {
          sub: "能耗管理",
          left: {
            left: () => import("@/components/left/energyManage/index.vue")
          },
          right: {
            right: () =>
              import(
                "@/components/right/energyManage/EnergyConsumStatistics.vue"
              )
          }
        }
      ],
      module: {
        alarmRecord: () => import("@/components/popups/alarmRecord.vue"),
        index: () => import("@/components/modules/index.vue"),
        videoPop: () => import("@/components/popups/video.vue"),
        mjPop: () => import("@/components/popups/mjinfo.vue"),
        equipmentinfo: () => import("@/components/popups/equipmentinfo.vue"),
        carInfo: () => import("@/components/popups/carInfo.vue"),
        investmentInfo: () => import("@/components/popups/investmentPop.vue"),
        equipment: () => import("@/components/popups/equipment.vue"),
        leftChange: () => import("@/components/modules/leftChange.vue"),
        histroyalarm: () => import("@/components/popups/histroyalarm.vue"),
        alarmalert: () => import("@/components/popups/alarmalert.vue"),
        videoWall: () => import("@/components/popups/videoWall.vue"),
        common: () => import("@/components/popups/common.vue"),
        help: () => import("@/components/popups/help.vue")
      },
      gisInit(webgis) {
        //返回地图对象
        window.Sgworld = webgis;
        window.isFir = false;
        // 场景成功加载,对象成功返回,清除蒙版
        try {
          window.loading.close();
        } catch (error) {
          console.log(error);
        }
        console.log("---------------------初始化完成------------------");
        // 定义一个统一的初始视角
        window.viewInit = function() {
          window.Sgworld &&
            window.Sgworld.flyTo(
              2428.97509765625,
              6744.2890625,
              500,
              0,
              -11.069870948791504,
              108.2399673461914,
              16229.828125,
              () => {},
              4
            );
        };
        window.UEGetAll = function() {
          var list = [
            "门禁",
            "信息发布",
            "暖通",
            "消防",
            "广播",
            "巡更",
            "停车系统",
            "照明",
            "变配电",
            "电梯",
            "报警管理",
            "空调系统",
            "变配电系统",
            "公共广播",
            "信息发布",
            "亮化系统",
            "电子巡更",
            "门禁管理",
            "入侵管理",
            "消防系统"
          ];
          window.Sgworld.execute("grapi", "GetALL", {}, function(res) {
            if (window.isFir) {
              var a = res.args.name && res.args.name.split("_");
              if (a[1] == "车位") {
                window.Sgworld.focusOn(
                  new TUVector3(
                    res.args.location.x,
                    res.args.location.y,
                    res.args.location.z
                  ),
                  new TURotator(0, 0, 0),
                  200,
                  () => {},
                  4
                );
              }
            }
            var hasPosition = res.args.ue4x;
            var newX, newY, newZ, newRoll, newPitch, newYaw;
            var b = res.args.name && res.args.name.split("-");
            // 根据当前点击的图标 获取当前点击的图标name
            // 类别-名称-别名
            var ppname = b[1]||[];
            if(!ppname.includes('_')){
              ppname = b[2];
            }
            if (hasPosition) {
              newX = res.args.ue4x;
              newY = res.args.ue4y;
              newZ = res.args.ue4z;
              var params = {
                name: ppname, //设备名称
                location: new TUVector3(newX, newY, newZ), //设备位置
                rotate: -45
              };
              axios.get("./static/json/PositionCalibration.json").then(res => {
                var positionCalibrationData = res.data;
                var tem = positionCalibrationData.find(item => {
                  return item.name === ppname;
                });
                // 若当前点击的节点在校验文件中
                // 从校验文件中取值，并通过focusOn方法定位
                if (tem) {
                  newRoll = tem.degree && tem.degree.roll;
                  newPitch = tem.degree && tem.degree.pitch;
                  newYaw = tem.degree && tem.degree.yaw;
                  window.Sgworld.focusOn(
                    new TUVector3(newX, newY, newZ),
                    new TURotator(newRoll, newPitch, newYaw),
                    200,
                    () => {},
                    2
                  );
                } else {
                  // 当前点击的不在校验文件中 通过 设备名称进行定位
                  window.Sgworld.execute(
                    "grapi",
                    "AIPawnLocation",
                    params,
                    null
                  );
                }
              });
            }
            if (b[0] == "视频") {
              eneryEquipmentQuery().then(response => {
                var alldata = response.data.data;
                var clickVideo = alldata.filter(item => {
                  return item.code && item.code == b[1];
                });
                var tem = {};
                tem.show = true;
                tem.title = b[2] == "" ? "-" + b[3] : b[2];
                tem.ele =
                  clickVideo.length > 0
                    ? clickVideo[0].cameraIndexCode
                    : undefined;
                window.showvideo(tem);
              });
            }
            if (b[0] == "紧急求助") {
              var tem = {};
              tem.show = true;
              tem.title = b[2] == "" ? "-" + b[3] : b[2];
              window.showHelp(tem);
            }
            if (list.includes(b[0])) {
              var info = {};
              info.title = b[0];
              info.lc = b[1];
              info.name = b[2];
              window.handelCommon(info);
            }
          });
        };

        // 显示车流、清除车位,招商,poi图标
        window.UEReset = function() {
          // 显示车流线
          window.Sgworld.execute(
            "grapi",
            "CarLine",
            { visibility: true },
            null
          );
          // 关闭显示招商信息
          window.Sgworld.execute(
            "grapi",
            "business",
            { visibility: false },
            null
          );
          // 关闭显示车位
          window.Sgworld.execute("grapi", "BackPark", {}, null);
          // 清除所有poi点位
          window.Sgworld.clearAllCovering();
          // var timetype = window.sessionStorage.getItem("timeType");
          // var weathertype = window.sessionStorage.getItem("weatherType");
          // window.Sgworld.execute("grapi", "settime", { type: timetype }, null);
          // window.Sgworld.execute(
          //   "grapi",
          //   "setweather",
          //   { type: weathertype },
          //   null
          // );
        };
        // 加载车流线
        window.Sgworld.execute("grapi", "CarLine", { visibility: true }, null);

        // 创建楼层标记
        window.labels = [];
        window.creatLabel = function(param) {
          if (window.labels.length !== 0) {
            for (let i = 0; i < window.labels.length; i++) {
              window.labels[i].removeFromMap();
            }
          }
          let item = window.Sgworld.createLabel({
            x: 225.14,
            y: -130.52,
            z: 1000, //位置 z
            size: { width: 120, height: 80 }, //标签尺寸 需要手动设置
            text: param, //标签文字
            distanceDisplayCondition: { x: 300, y: 500 },
            type: "Type1", //标签类型
            scale: 1,
            backgroundColor: "rgba(0,238,255,0)",

            fillColor: "#00ff00"
          });
          window.labels.push(item);
        };
        // 清除楼层标记
        window.clearLabel = function() {
          if (window.labels.length !== 0) {
            for (let i = 0; i < window.labels.length; i++) {
              window.labels[i].removeFromMap();
            }
          }
        };
        // 执行getAll方法,获取UE回调中的信息
        window.UEGetAll();

        // 执行viewInit方法,飞到固定角度
        window.viewInit();
      }
    });
  },
  methods: {
    getTree() {
      var count = 0;
      var get_data = [
        {
          id: 1,
          label: "全部设备",
          disabled: true,
          children: [
            { id: "spjk", label: "视频监控", children: [], rootType: "视频" },
            { id: "mjgl", label: "门禁管理", children: [], rootType: "门禁" },
            { id: "dzxg", label: "电子巡更", children: [], rootType: "巡更" },
            {
              id: "tcxt",
              label: "停车系统",
              children: [],
              rootType: "停车系统"
            },
            {
              id: "jjqz",
              label: "紧急求助",
              children: [],
              rootType: "紧急求助"
            },
            { id: "gggb", label: "公共广播", children: [], rootType: "广播" },
            {
              id: "xxfb",
              label: "信息发布",
              children: [],
              rootType: "信息发布"
            },
            { id: "ntxt", label: "楼控系统", children: [], rootType: "暖通" },
            { id: "xfxt", label: "消防系统", children: [], rootType: "消防" },
            { id: "zmxt", label: "亮化系统", children: [], rootType: "照明" },
            { id: "dtxt", label: "电梯系统", children: [], rootType: "电梯" },
            {
              id: "bpdxt",
              label: "变配电系统",
              children: [],
              rootType: "变配电"
            },
            {
              id: "rqxt",
              label: "人脸识别系统",
              children: [
                { id: "rq1", label: "2F_人脸识别_05" },
                { id: "rq2", label: "2F_人脸识别_12" }
              ],
              rootType: "入侵"
            }
          ]
        }
      ];
      var htData = [];
      var ueData = [];
      var vedioArr = [];
      var accessArr = [];
      var dzxgArr = [];
      var tcxtArr = [];
      var jjqzArr = [];
      var infoArr = [];
      var airArr = [];
      var fireArr = [];
      var broadArr = [];
      var zmxtArr = [];
      var dtArr = [];
      var bpdArr = [];
      var rqArr = [];
      // 获取后台数据
      axios.post(window.apiUrl + "/api/queryTree/").then(res => {
        htData = res.data.data;
        // 获取UE数据
        axios.get("./static/json/allUEEquipment.json").then(res => {
          ueData = res.data;
          for (var i = 0; i < ueData.length; i++) {
            for (var j = 0; j < htData.length; j++) {
              count++;
              if (htData[j].code == ueData[i].name) {
                ueData[i] = { ...ueData[i], ...htData[j] };
                break;
              }
            }
            var temObj = ueData;
            // 设备所在楼层
            var LC = temObj[i].code ? temObj[i].code.substring(0, 2) : "无";
            // 设备的名称
            var MX = temObj[i].code ? temObj[i].code.substring(3) : "无";
            // 设备所在的位置
            // var tposition = {
            //   ...temObj[i].location,
            //   roll: 0,
            //   pitch: 0,
            //   yaw: 0,
            // };
            var tposition = {
              ...temObj[i].location,
              ...temObj[i].degree
            };
            var item = {
              id: MX,
              label: temObj[i].name,
              floorId: LC,
              // name: temObj[i].name,
              pid: temObj[i].pid,
              code: temObj[i].code,
              buildName: "展示中心大楼",
              position: tposition
            };
            if (temObj[i].code) {
              if (
                temObj[i].code.indexOf("摄像") != -1 ||
                temObj[i].code.indexOf("QXSXT") != -1
              ) {
                item.rootType = "视频";
                var sta = vedioArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var vedioArrItem = {
                    id: "视频" + LC,
                    label: LC,
                    children: [],
                    rootType: "视频",
                    lc: LC
                  };
                  vedioArrItem.children.push(item);
                  vedioArr.push(vedioArrItem);
                }
                get_data[0].children[0].children = vedioArr;
              }
              if (temObj[i].code.indexOf("门禁") != -1) {
                item.rootType = "门禁";
                var sta = accessArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var accessArrItem = {
                    id: "门禁" + LC,
                    label: LC,
                    children: [],
                    rootType: "门禁",
                    lc: LC
                  };
                  accessArrItem.children.push(item);
                  accessArr.push(accessArrItem);
                }
                get_data[0].children[1].children = accessArr;
              }
              if (temObj[i].code.indexOf("信息发布") != -1) {
                item.rootType = "信息发布";
                var sta = infoArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var infoArrItem = {
                    id: "信息发布" + LC,
                    label: LC,
                    children: [],
                    rootType: "信息发布",
                    lc: LC
                  };
                  infoArrItem.children.push(item);
                  infoArr.push(infoArrItem);
                }
                get_data[0].children[6].children = infoArr;
              }
              if (
                temObj[i].code.indexOf("XF") != -1 ||
                temObj[i].code.indexOf("AHU") != -1 ||
                temObj[i].code.indexOf("PF") != -1
              ) {
                item.rootType = "暖通";
                var sta = airArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var airArrItem = {
                    id: "暖通" + LC,
                    label: LC,
                    children: [],
                    rootType: "暖通",
                    lc: LC
                  };
                  airArrItem.children.push(item);
                  airArr.push(airArrItem);
                }
                get_data[0].children[7].children = airArr;
              }
              if (
                temObj[i].code.indexOf("消防") != -1 ||
                temObj[i].code.indexOf("报警器") != -1 ||
                temObj[i].code.indexOf("灭火装置") != -1
              ) {
                item.rootType = "消防";
                var sta = fireArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var fireArrItem = {
                    id: "消防" + LC,
                    label: LC,
                    children: [],
                    rootType: "消防",
                    lc: LC
                  };
                  fireArrItem.children.push(item);
                  fireArr.push(fireArrItem);
                }
                get_data[0].children[8].children = fireArr;
              }
              if (temObj[i].code.indexOf("喇叭") != -1) {
                item.rootType = "广播";
                var sta = broadArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var broadArrItem = {
                    id: "广播" + LC,
                    label: LC,
                    children: [],
                    rootType: "广播",
                    lc: LC
                  };
                  broadArrItem.children.push(item);
                  broadArr.push(broadArrItem);
                }
                get_data[0].children[5].children = broadArr;
              }
              if (temObj[i].code.indexOf("巡检点") != -1) {
                item.rootType = "巡更";
                var sta = dzxgArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var dzxgArrItem = {
                    id: "巡更" + LC,
                    label: LC,
                    children: [],
                    rootType: "巡更",
                    lc: LC
                  };
                  dzxgArrItem.children.push(item);
                  dzxgArr.push(dzxgArrItem);
                }
                get_data[0].children[2].children = dzxgArr;
              }
              if (
                temObj[i].code.indexOf("车位") != -1 ||
                temObj[i].code.indexOf("查询一体机") != -1 ||
                temObj[i].code.indexOf("引导显示屏") != -1
              ) {
                item.rootType = "停车系统";
                var sta = tcxtArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var tcxtArrItem = {
                    id: "停车系统" + LC,
                    label: LC,
                    children: [],
                    rootType: "停车系统",
                    lc: LC
                  };
                  tcxtArrItem.children.push(item);
                  tcxtArr.push(tcxtArrItem);
                }
                get_data[0].children[3].children = tcxtArr;
              }
              if (temObj[i].code.indexOf("残卫") != -1) {
                item.rootType = "紧急求助";
                var sta = jjqzArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var jjqzArrItem = {
                    id: "紧急求助" + LC,
                    label: LC,
                    children: [],
                    rootType: "紧急求助",
                    lc: LC
                  };
                  jjqzArrItem.children.push(item);
                  jjqzArr.push(jjqzArrItem);
                }
                get_data[0].children[4].children = jjqzArr;
              }
              if (temObj[i].code.indexOf("灯") != -1) {
                item.rootType = "照明";
                var sta = zmxtArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var zmxtArrItem = {
                    id: "照明" + LC,
                    label: LC,
                    children: [],
                    rootType: "照明",
                    lc: LC
                  };
                  zmxtArrItem.children.push(item);
                  zmxtArr.push(zmxtArrItem);
                }
                get_data[0].children[9].children = zmxtArr;
              }
              if (temObj[i].code.indexOf("电梯") != -1) {
                item.rootType = "电梯";
                dtArr.push(item);
                get_data[0].children[10].children = dtArr;
              }
              if (
                temObj[i].code.indexOf("变配电") != -1 ||
                temObj[i].code.indexOf("强电室") != -1
              ) {
                item.rootType = "变配电";
                var sta = bpdArr.filter(node => {
                  return node.lc == LC;
                });
                if (sta.length > 0) {
                  sta[0].children.push(item);
                } else {
                  var bpdArrItem = {
                    id: "变配电" + LC,
                    label: LC,
                    children: [],
                    rootType: "变配电",
                    lc: LC
                  };
                  bpdArrItem.children.push(item);
                  bpdArr.push(bpdArrItem);
                }
                get_data[0].children[11].children = bpdArr;
              }
            }
            if (i == ueData.length - 1) {
              if (j == htData.length) {
                // 保存数据
                window.sessionStorage.setItem(
                  "treeData",
                  JSON.stringify(get_data)
                );
                window.getTree();
                window.getTreeAlarm();
                window.getTreeDevice();
                console.log("data-success-load!");
                return JSON.stringify(get_data);
              }
            }
          }
        });
      });
    },
    // 树的广度遍历
    treeForeach(tree, func) {
      let node,
        list = [...tree];
      while ((node = list.shift())) {
        func(node);
        node.children && list.push(...node.children);
      }
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>
